import sys
import struct
import os

try :
	in_filename = sys.argv[1]
	out_filename = sys.argv[2]
except :
	print sys.argv[0] + ' <map_textfile> <map_file>'
	sys.exit()



fh = open(in_filename, 'r')
raw_file = fh.read()
fh.close()



outfile = ''



lines = raw_file.split('\n')
if lines[-1] == '' :
	del lines[-1]
rows = []
for line in lines :
	rows.append(line.split(' '))
	
print 'width: ' + str(len(rows))
print 'height: ' + str(len(lines))
	
outfile += struct.pack('H', len(rows))
outfile += struct.pack('H', len(lines))



for line in raw_file.split('\n') :
	for map_coord in line.split(' ') :
		if len(map_coord) > 0 :
			pieces = map_coord.split(',')
			outfile += struct.pack('H', int(pieces[0]))
			outfile += struct.pack('B', int(pieces[1]))

print str(len(outfile)) + ' bytes'

fh = open(out_filename, 'wb')
fh.write(outfile)
fh.close()
